<?

//Initiate Authenticate
SecurityRedirect ();

//initiate Vars 
define("PRIMARY_KEY","letter_id");
$FileName        = "ais_transaction_lettera.php";
$FileTemplate    = "ais_transaction_lettera_form.html";
$HTMLFormName    = "CurrencyForm";
$TableName       = "ais_letter";
$SubHeaderTitle  = "Transaction Letter - Last Notification Letter";
$FormAction      = $FileName . "?" . AddParam(GetQueryString("QueryString", ""),"form","$HTMLFormName");
$FormEncType     = "application/x-www-form-urlencoded";
//$PMode         = "";

//Initiate Database Table For Insert & Update;
$FormFields     = array("letter_id"=>"",
						"letter_code"=>"",
						"to_attention"=>"",
						"to_company"=>"",
						"lampiran"=>"",
						"no"=>"",
						"perihal"=>"",
						"place"=>"",
						"notify_date"=>"date");
buildsqlfield ($TableName, $FormFields);

//Leave Here Untouch
$FormCName     = BuildHTMLField($FormFields,"name",1);
$FormCValue    = BuildHTMLField($FormFields,"value",1);
$letter_id     = GetParam("letter_id","");

//************ FORM ACTION **************
if (GetParam("form","")==$HTMLFormName) {
	if (GetParam("Action","")=="Add") {
		$FormMode="Edit";
		FormInsert();
	}
	if (GetParam("Action","")=="Update") {
		$FormMode="Edit";
		FormUpdate();
	}
	if (GetParam("Action","")=="Delete") {
		$FormMode="Edit";
		FormDelete();
	}
	if (GetParam("Action","")=="Cancel") {
		$FormMode="Edit";
		FormCancel();
	}
}

//Field Verification 
$Verification  = "Enable";
$SessionVar    = md5(microtime());
//$SessionVar    = "Stuck";
$VerifyFields  = array("notify_date"=>"Date","to_attention"=>"Attention To");
VerifyHTML ($HTMLFormName,$VerifyFields,$SessionVar);

$InitFunction = "Masking()";
Init($SessionVar,BuildMasking ($HTMLFormName,$FormFields));
//************ SET FORM MODE ***************

if (GetParam(PRIMARY_KEY,"")) $FormMode = "Edit";
else $FormMode = "Add";

//******* DATABASE FORM QUERY ***********
$SQLCurrency = "SELECT * FROM tblcurrency";

$SQL = "SELECT * FROM `$TableName` ";
if (GetParam(PRIMARY_KEY,"")) {
    $SQLForm = $SQL."WHERE ".PRIMARY_KEY."='".GetParam(PRIMARY_KEY,"")."'";
}
//******* SHOW FORM CONTENT *********
$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);

$TBS->MergeBlock("currblk",$DBMysql,$SQLCurrency);

if (GetParam(PRIMARY_KEY,"")) $TBS->MergeBlock("blv","mysql",$SQLForm);
else $TBS->MergeBlock("blv",$FormCValue);
//HTML Name
$TBS->MergeBlock("blc",$DBMysql,$SQL);
$TBS->MergeBlock("bln",$FormCName);
//HTMLGrid
$TBS->MergeBlock("blg",$DBMysql,$SQL);
$TBS->Show();


//*************** ACTION FUNCTION *******************
function FormInsert() {
	global $DBConnection,$FileName;
	$DBConnection->Insert();
	header("Location: $FileName?PageMode=Form");
	exit;
}

function FormUpdate() {
	global $DBConnection;
	$DBConnection->Update();

}

function FormDelete() {
	global $DBConnection,$FileName,$TableName;
	$DBConnection->Delete("$TableName",PRIMARY_KEY);
	header("Location: $FileName");
	exit;	
	
}

function FormSearch () {

}

function FormCancel () {
	global $FileName;
	header("Location: ".$FileName);
	exit;
}
//*************** USER FUNCTION *******************
function buildsqlfield ($table, $arrayfield) {
		global $DBConnection;
		foreach ($arrayfield as $key=>$Formatting) {
			$value = GetParam($key,"");
			if ($Formatting=="date") {
				list($day,$month,$year) = explode("/",$value);
				$value = "$year-$month-$day";
			} elseif ($Formatting=="number") $value = str_replace(",","",$value);
			$DBConnection->add_htmlfield($table,$key,$value);
		}
}

?>